<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="95px">
      <el-form-item label="学段名称" prop="schoolLevel">
        <el-input
          v-model="queryParams.schoolLevel"
          placeholder="请输入学段名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否寄宿" prop="islodge">
        <el-switch v-model="isOnLodge" />
      </el-form-item>
      <el-form-item label="是否强制TST" prop="isforceTst">
        <el-switch v-model="isOnTst" />
      </el-form-item>
      <el-form-item label="文档编号" prop="showorder">
        <el-input
          v-model="queryParams.showorder"
          placeholder="请输入就读学段的编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['system:schoolLevel:add']"
        >添加</el-button>
      </el-form-item>
<!--      <el-form-item label="创建时间" prop="createdTime">
        <el-date-picker clearable
          v-model="queryParams.createdTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="更新时间" prop="updatedTime">
        <el-date-picker clearable
          v-model="queryParams.updatedTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择更新时间">
        </el-date-picker>
      </el-form-item>-->
    </el-form>

    <el-table v-loading="loading" :data="schoolLevelList" @selection-change="handleSelectionChange" border>
      <el-table-column type="selection" width="55" align="center" />
     <el-table-column label="ID" align="center" prop="id" /> 
      <el-table-column label="学段名称" align="center" prop="schoolLevel" />
      <el-table-column label="是否寄宿" align="center" prop="islodge" >
        <el-switch v-model="isOnLodgeItem" />
        </el-table-column>
      <el-table-column label="强制TST" align="center" prop="isforceTst">
        <el-switch v-model="isOnTstItem" />
        </el-table-column>
      <!-- <el-table-column label="文档编号" align="center" prop="showorder"/> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" @click="handleUpdate(scope.row)" v-hasPermi="['system:schoolLevel:edit']">修改</el-button>
          <el-button link type="primary" @click="handleDelete(scope.row)" v-hasPermi="['system:schoolLevel:remove']">删除</el-button>
          <el-button link type="primary" @click="handleReview(scope.row)">查看文档</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改学校学段管理对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="schoolLevelRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="学段名称" prop="schoolLevel">
          <el-input v-model="form.schoolLevel" placeholder="请输入学段名称" />
        </el-form-item>
        <el-form-item label="是否寄宿" prop="islodge">
          <el-switch v-model="isOnLodgeItem" />
        </el-form-item>
        <el-form-item label="是否强制TST" prop="isforceTst">
          <el-switch v-model="isOnTstItem" />
        </el-form-item>
        <el-form-item label="文档编号" prop="showorder">
          <el-input v-model="form.showorder" placeholder="请输入文档编号" />
          </el-form-item>
        <!-- <el-form-item label="创建时间" prop="createdTime">
          <el-date-picker clearable
            v-model="form.createdTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择创建时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="更新时间" prop="updatedTime">
          <el-date-picker clearable
            v-model="form.updatedTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择更新时间">
          </el-date-picker>
        </el-form-item> -->
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 查看文档编号对话框 -->
    <el-dialog title="查看文档信息" v-model="showorderVisible" width="500px" append-to-body>
      <el-form ref="showorderRef" :model="showorderForm" :rules="showorderRules" label-width="80px">
<el-form-item label="文档编号" prop="showorder">
  <el-input v-model="showorderForm.showorder" disabled/>
</el-form-item>
</el-form>
</el-dialog>
  </div>
</template>

<script setup name="SchoolLevel">
import { listSchoolLevel, getSchoolLevel, delSchoolLevel, addSchoolLevel, updateSchoolLevel } from "@/api/system/schoolLevel";
import axios from "axios";
const { proxy } = getCurrentInstance();

const schoolLevelList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const isOnLodge=ref(true);
const isOnTst=ref(true);
const isOnLodgeItem=ref(true);
const isOnTstItem=ref(true);
const showorderVisible=ref(false);
const showorderForm=ref({
  showorder:''
});
const showorderRules=ref({});
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    schoolLevel: null,
    islodge: null,
    isforceTst: null,
    showorder: null,
    createdTime: null,
    updatedTime: null
  },
  rules: {
  }
});

const { queryParams, form, rules} = toRefs(data);

 // 查看文档信息
 const handleReview = async (row) => {
      try {
        const res = await axios.get(`/system/schoolLevel/${row.showorder}`);
        showorderForm.value.showorder = row.showorder;
        showorderVisible.value = true;
      } catch (error) {
        console.error('请求失败:', error);
      }
    };

/** 查询学校学段管理列表 */
function getList() {
  loading.value = true;
  listSchoolLevel(queryParams.value).then(response => {
    schoolLevelList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    schoolLevel: null,
    islodge: null,
    isforceTst: null,
    showorder: null,
    createdTime: null,
    updatedTime: null
  };
  proxy.resetForm("schoolLevelRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加学校学段管理";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getSchoolLevel(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改学校学段管理";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["schoolLevelRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateSchoolLevel(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addSchoolLevel(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除学校学段管理编号为"' + _ids + '"的数据项？').then(function() {
    return delSchoolLevel(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('system/schoolLevel/export', {
    ...queryParams.value
  }, `schoolLevel_${new Date().getTime()}.xlsx`)
}

getList();
</script>
